﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

using TLL.Repository;
using TLL.Debug;

namespace TLL.Log
{
    public class LogProjectRepository : XMLRepository<LogProject>
    {
        private int lastID = 0;
        public LogProjectRepository(string dataURL, string debugURL, NotifyIcon notify) : base(dataURL, debugURL, true, true, XMLDirectionType.Local, notify) { }
        public override List<LogProject> Get(int id = 0)
        {
            var res = new List<LogProject>();
            try
            {
                if (id == 0)
                    foreach (XmlNode node in this.Document.ChildNodes[1])
                        res.Add(new LogProject(
                                this.AttributeByType<int>(node, XMLLogAttributes.ID),
                                this.AttributeByType<string>(node, XMLLogAttributes.Name)
                            ));
                else
                    foreach (XmlNode node in this.Document.ChildNodes[1])
                        if (this.AttributeByType<int>(node, XMLLogAttributes.ID) == id)
                        {
                            res.Add(new LogProject(
                                    this.AttributeByType<int>(node, XMLLogAttributes.ID),
                                    this.AttributeByType<string>(node, XMLLogAttributes.Name)
                                ));
                            break;
                        }
            }
            catch
            {
                this.Error = new Error(ErrorType.Get, new Dictionary<Enum, string>() { 
                    { XMLLogAttributes.ID, id.ToString() }
                });
                DebugLogRepository.Add(InformationType.Error, string.Format(@"LogProjectRepository, Get(id = {0})", id), this.DebugURL); 
            }
            DebugLogRepository.Add(InformationType.Information, string.Format(@"LogProjectRepository, Get(int id = {0}). Success operation", id), this.DebugURL);
            return res;
        }
        public override bool Create(LogProject item)
        {
            try
            {
                this.Document.ChildNodes[1].AppendChild(this.Create_Node(
                    "Project",
                    this.Create_Attributes(new Dictionary<Enum, string>() { 
                        { XMLLogAttributes.ID, this.lastID.ToString() },
                        { XMLLogAttributes.Name, item.Name }
                    })
                ));
                this.lastID++;
            }
            catch 
            {
                this.Error = new Error(ErrorType.Create, new Dictionary<Enum, string>() { 
                        { XMLLogAttributes.ID, lastID.ToString() },
                        { XMLLogAttributes.Name, item.Name }
                });
                this.Warning = new Warning(
                    WarningType.Create,
                    string.Format("Error creating LogProject"),
                    this.Notify,
                    delegate(object sender, EventArgs e)
                    {
                        this.Error.Show(DisplayDebugType.MessageBox);
                    });
                DebugLogRepository.Add(InformationType.Error, string.Format(@"LogProjectRepository, Create(LogProject item[ID = {0}])
                                           Error title: {1}
                                           Error message: {2}", item.ID, Error.Title, Error.Message), this.DebugURL);
                return false; 
            }
            DebugLogRepository.Add(InformationType.Error, string.Format(@"LogProjectRepository, Create(LogProject item[ID = {0}])
                                           Error title: {1}
                                           Error message: {2}", item.ID, Error.Title, Error.Message), this.DebugURL);
            return true;
        }
        public override bool Delete(int id)
        {
            try
            {
                foreach (XmlNode node in this.Document.ChildNodes[1])
                    if (this.AttributeByType<int>(node, XMLLogAttributes.ID) == id)
                        this.Document.ChildNodes[1].RemoveChild(node);
                DebugLogRepository.Add(InformationType.Information, string.Format(@"LogProjectRepository, Delete(int id = {0}). Success operation", id), this.DebugURL);
                return true;
            }
            catch
            {
                this.Error = new Error(ErrorType.Delete, new Dictionary<Enum, string>() { 
                        { XMLLogAttributes.ID, id.ToString() },
                });
                this.Warning = new Warning(
                    WarningType.Delete,
                    string.Format("Error deleting LogProject"),
                    this.Notify,
                    delegate(object sender, EventArgs e)
                    {
                        this.Error.Show(DisplayDebugType.MessageBox);
                    });
                DebugLogRepository.Add(InformationType.Error, string.Format(@"LogProjectRepository, Delete(int id = {0})
                                       Error title: {2}
                                       Error message: {3}", id), this.DebugURL);
                return false;
            }
        }

        private int GetLastID()
        {
            var items = Get().Select(x => x.ID).OrderBy(x => x).ToList();
            if (items.Count > 0)
                return items.LastOrDefault() + 1;
            else
                return 0;
        }
    }
}
